{% set datevalue = 0|date %}
{% if header.lsys %}
logical-systems {{ header.lsys }} {
{% do header.update({'lsys': "" }) %}
{% import "junos.j2" as temp with context %}
{% set first = 0 %}
{% set filter = "%s"|format(temp) %}
{{ filter|indent(4, True) }}
}
{% endif %}
{% if first %}
firewall {
   family inet {
      replace:
      /*
       * $Id: {{ filename }} $
       * $Date: {{ datevalue }} $
       *
       * {{ header.comment }}
       */
      filter {{ header.filter }} {
         {% if header.intsp %}
         interface-specific;
         {% endif %}
         {% for term in terms %}
         {% if term.comment %}
         /* 
          * {{ term.comment }}
          */
         {% endif %}
         term {{ term.name }} {
            {% if term.from %}
            from {
               {% if term.srcprefix %}
               source-prefix-list {
                  {% for item in term.srcprefix %}
                  {{ item }};
                  {% endfor %}
                  {% for item in term.srcpflex %}
                  {{ item }} except;
                  {% endfor %}
               }
               {% endif %}
               {% if term.srcaddr %}
               source-address {
                  {% for item in term.srcaddr %}
                  {{ item }};
                  {% endfor %}
                  {% for item in term.srcadex %}
                  {{ item }} except;
                  {% endfor %}
               }
               {% endif %}
               {% if term.dstprefix %}
               destination-prefix-list {
                  {% for item in term.dstprefix %}
                  {{ item }};
                  {% endfor %}
                  {% for item in term.dstpflex %}
                  {{ item }} except;
                  {% endfor %}
               }
               {% endif %}
               {% if term.dstaddr %}
               destination-address {
                  {% for item in term.dstaddr %}
                  {{ item }};
                  {% endfor %}
                  {% for item in term.dstadex %}
                  {{ item }} except;
                  {% endfor %}
               }
               {% endif %}
               {% if term.protocol %}
               {% if term.protocol|length == 1 %}
               protocol {{ term.protocol|last }};
               {% else %}
               protocol [ {{ term.protocol|join(" ") }} ];
               {% endif %}
               {% endif %}
               {% if term.srcport %}
               {% if term.srcport|length == 1 %}
               source-port {{ term.srcport|last }};
               {% else %}
               source-port [ {{ term.srcport|join(" ") }} ];
               {% endif %}
               {% endif %}
               {% if term.dstport %}
               {% if term.dstport|length == 1 %}
               destination-port {{ term.dstport|last }};
               {% else %}
               destination-port [ {{ term.dstport|join(" ") }} ];
               {% endif %}
               {% endif %}
               {% if term.option %}
               {{ term.option }};
               {% endif %}
               {% if term.icmptypes %}
               {% if term.icmptypes|length == 1 %}
               icmp-type {{ term.icmptypes|last }}
               {% else %}
               icmp-type [{{ term.icmptypes|join(" ") }}];
               {% endif %}
               {% endif %}
               {% if term.ttl %}
               ttl {{ term.ttl }};
               {% endif %}
               {% if term.ttlexcept %}
               ttl-except {{ term.ttlexcept }};
               {% endif %}
            }
            {% endif %}
            then {
               {% if term.counter %}
               counter {{ term.counter }};
               {% endif %}
               {% if term.policer %}
               policer {{ term.policer }};
               {% endif %}
               {{ term.action }};
            }
         }
         {% endfor %}
      }
   }
}
{% if pfls %}
policy-options {
   {% for prefix in pfls %}

   replace:
   prefix-list {{ prefix.name }} {
      {% for ix in prefix.ip %}
      {{ ix }};
      {% endfor %}
   }
   {% endfor %}
}
{% endif %}
{% endif %}
